#!/bin/bash

function info() {
    echo -e "\033[1;34m$1 \033[0m"
}

function warn() {
    echo  -e "\033[0;33m$1 \033[0m"
}

function error() {
    echo  -e "\033[0;31m$1 \033[0m"
}

function createGoPath() {
    if [ ! -d $1 ];
        then
        mkdir -p $1
    fi
    if [ ! -d "$1/src" ];
    then
        mkdir "$1/src"
    fi
    if [ ! -d "$1/bin" ];
    then
        mkdir "$1/bin"
    fi
    if [ ! -d "$1/pkg" ];
    then
        mkdir "$1/pkg"
    fi
}

function initGOROOT(){
    if [ ! -n $GOROOT ];
    then
        warn "not exists GOROOT"
        exit
    fi

    gopath="`pwd`"
    warn "Use $gopath as golang workspace..."
    info "export GOPATH=$gopath"
    exportGopath="GOPATH=$gopath"
    export $exportGopath    
}

function buildAutoBidding(){
    info "go build autobidding"
    cd "$gopath/src/lpaiche.com/autobidding/"
    go install    
}

function buildBailconsumer(){
    info "go build bailconsumer"
    cd "$gopath/src/lpaiche.com/bailconsumer/"
    go install
    cp $gopath/src/lpaiche.com/bailconsumer/conf/* $gopath/bin/conf/    
}

function buildHammer(){
    info "go build hammer"
    cd "$gopath/src/lpaiche.com/hammer/"
    go install
    cp $gopath/src/lpaiche.com/hammer/conf/* $gopath/bin/conf/    
}

function biddingconsumer(){
    info "go build biddingconsumer"
    cd "$gopath/src/lpaiche.com/biddingconsumer/"
    go install
    cp $gopath/src/lpaiche.com/biddingconsumer/conf/* $gopath/bin/conf/    
}

initGOROOT;
case "$1" in
    autobidding)
        buildAutoBidding
        ;;
    bailconsumer)
        buildBailconsumer
        ;;
    auctionHammer)
        buildHammer
        ;;
    bidding)
        biddingconsumer
        ;;
    *)
        echo $"Usage: $0 {autobidding|bailconsumer|auctionHammer|bidding}"
        RETVAL=3
esac
